package kr.ac.chungbuk;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BoardController {
	ArrayList<BoardDTO> boardList = new ArrayList<BoardDTO>();
	IBoardDAO ibaties = new IBoardDAO();
	@RequestMapping("reserInsert")
	public String reserInsert(){
		return "../views/question/reserInsert";
	}
/*	@RequestMapping(value="writeProc", method=RequestMethod.POST)
	public String writeProc(HttpServletRequest request){
		try {
			request.setCharacterEncoding("euc-kr");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		
		BoardDTO boardDTO = new BoardDTO();
		boardDTO.setTitle(title);
		boardDTO.setContent(content);
		boardList.add(boardDTO);
		ibaties.insert(boardDTO);
		return "writeProc";
	}*/
	
	@RequestMapping(value="insertFormProc", method=RequestMethod.GET)
	public ModelAndView insertFormProc(HttpServletRequest request, HttpSession session){
		UserDTO user=(UserDTO)session.getAttribute("user");
		ModelAndView mav = new ModelAndView();
		try {
			request.setCharacterEncoding("euc-kr");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}if(user==null){
			mav.setViewName("../views/question/reservation");
		}else{
			System.out.println(user.getUser_Id());
			String title = request.getParameter("title");
			System.out.println(title);
			String content = request.getParameter("content");
			String user_Id = user.getUser_Id();
			String user_Name = user.getUser_Name();
			Timestamp update_time=getCurrentTime();
			BoardDTO boardDTO=new BoardDTO(title, content, user_Id, user_Name, update_time);
			boardList.add(boardDTO);
			mav.addObject("boardList", boardList);
			ibaties.insert(boardDTO);
			mav.setViewName("redirect:reservation");
		}
		return mav;
	}
	private Timestamp getCurrentTime(){
		return new Timestamp(Calendar.getInstance().getTimeInMillis());
	}
	@RequestMapping("reservation")
	public ModelAndView reservation(ModelAndView mav){
		List list=ibaties.selectAll();
		mav.addObject("boardList",list);
		mav.setViewName("../views/question/reservation");
		return mav;
	}
	@RequestMapping("detailTitle")
	public ModelAndView detailTitle(String title, ModelAndView mav){
		BoardDTO list=ibaties.selectOne(title);
		mav.setViewName("../views/question/detailTitle");
		mav.addObject("boardList",list);
		return mav;
	}
	@RequestMapping(value="delete", method=RequestMethod.GET)
	public ModelAndView delete(String title,HttpSession session){
		UserDTO user=(UserDTO)session.getAttribute("user");
		ModelAndView mav = new ModelAndView();
		if(user==null){
			mav.setViewName("../views/question/reservation");
		}else{
		ibaties.delete(title);}
		mav.setViewName("redirect:reservation");
		return mav;
	}
	@RequestMapping("updateForm")
	public ModelAndView updateForm(String title, ModelAndView mav){
		BoardDTO board =ibaties.selectOne(title);
		mav.addObject("boardList", board);
		mav.setViewName("../views/question/updateForm");
		return mav;		
	}
	@RequestMapping(value="updateFormProc", method=RequestMethod.GET)
	public String updateFormProc(BoardDTO board){
		System.out.println(board.getTitle());
		ibaties.update(board);
		return "redirect:reservation";
	}
	
}